import {reqCategoryList,reqBannerList, reqFloorList} from '@/api'

// state内部的数据,不是永久存储,项目重新启动(包括手动刷新),state会率先重新初始化,
const state = {
    // 初始化数据
    categoryList:[],
    bannerlist:[],
    floorList:[]
}

// mutations里就是直接修改数据
const mutations = {
    RECEIVE_CATEGORYLIST(state, categoryList){
        state.categoryList = categoryList
    },

    RECEIVE_BANNERLIST(state,bannerList){
        state.bannerList = bannerList
    },
    
    RECEIVE_FLOORLIST(state,floorList){
    state.floorList = floorList
    }
}

// actions 里一般做的异步请求
const actions = {
    //异步请求获取数据,提交给mutations,修改state的对应数据

    // getCategoryList({commit}){
    //     reqCategoryList().then((response)=>{
    //         commit(RECEIVE_CATEGORYLIST,response.data)
    //     })
    // }

    // async await(等待promise的成功结果) 可以使用同步代码解决异步效果
    async getCategoryList({commit}){
        const result = await reqCategoryList()
        if(result.code === 200){
            commit('RECEIVE_CATEGORYLIST',result.data)
        }
    },

    async getBannerList({commit}){
        const result = await reqBannerList()
        if(result.code === 200){
            commit('RECEIVE_BANNERLIST', result.data)
        }
    },

    async getFloorList({commit}){
        const result = await reqFloorList()
        if(result.code === 200){
            commit('RECEIVE_BANNERLIST', result.data)
        }
    }
}


const getters = {}

export default {
    state,
    mutations,
    actions,
    getters
}